package cn.lili.controller.promotion;

import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.promotion.entity.dos.GreenContributionInfo;
import cn.lili.modules.promotion.entity.vos.GreenUserVO;
import cn.lili.modules.promotion.service.GreenContributionInfoService;
import cn.lili.modules.promotion.service.GreenCountService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pojos.GOrder;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author xiangqian
 * @version 1.0
 * @data 2023/12/20 16:40
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/promotion/green/user")
public class GreenUserController {

    private final GreenCountService greenCountService;
    private final GreenContributionInfoService greenContributionInfoService;

    /**
     * H5-绿色积分首页
     *
     * @return 返回结果
     */
    @GetMapping("/home")
    public ResultMessage<GreenUserVO> home() {
        AuthUser currentUser = UserContext.getCurrentUser();
        return ResultUtil.data(greenCountService.home(currentUser.getOpenId(), currentUser.getExtendId()));
    }

    /**
     * H5-绿色积分池明细
     *
     * @param greenContributionInfo 查询条件{@link GreenContributionInfo}
     * @param page                  分页条件{@link PageVO}
     * @return 分页返回结果
     */
    @GetMapping("/getGreenPoolInfo")
    public ResultMessage<Page<GreenContributionInfo>> getGreenPoolInfo(GreenContributionInfo greenContributionInfo, PageVO page) {
        return ResultUtil.data(greenContributionInfoService.infoPage(greenContributionInfo, page));
    }

    /**
     * H5-绿色贡献值明细
     *
     * @param greenContributionInfo 查询条件{@link GreenContributionInfo}
     * @param page                  分页条件{@link PageVO}
     * @return 分页返回结果
     */
    @ApiModelProperty("")
    @GetMapping("/getGreenContributionInfo")
    public ResultMessage<Page<GreenContributionInfo>> getGreenContributionInfo(GreenContributionInfo greenContributionInfo, PageVO page) {
        AuthUser currentUser = UserContext.getCurrentUser();
        greenContributionInfo.setUserId(currentUser.getExtendId());
        greenContributionInfo.setQueryType("1");
        return ResultUtil.data(greenContributionInfoService.infoPage(greenContributionInfo, page));
    }

    /**
     * H5-绿色积分明细
     *
     * @param gOrder 查询条件{@link GOrder}
     * @param page   分页条件{@link PageVO}
     * @return 分页返回结果
     */
    @GetMapping("/getGreenInfo")
    public ResultMessage<Page<GOrder>> getGreenInfo(GOrder gOrder, PageVO page) {
        AuthUser currentUser = UserContext.getCurrentUser();
        gOrder.setOpenid(currentUser.getOpenId());
        return ResultUtil.data(greenCountService.getGreenInfo(gOrder, page));
    }

    /**
     * H5-领取奖金
     *
     * @param contributionAllValue 要领取的贡献值
     * @return 返回结果
     */
    @GetMapping("/getBonus")
    public ResultMessage<Boolean> getBonus(BigDecimal contributionAllValue) {
        AuthUser currentUser = UserContext.getCurrentUser();
        return ResultUtil.data(greenCountService.getBonus(contributionAllValue, currentUser.getOpenId(), currentUser.getExtendId()));
    }

    /**
     * 计算可得绿色积分
     * @param contributionAllValue 需要结算的佣金
     * */
    @GetMapping("/computeBonus")
    public ResultMessage<BigDecimal> computeBonus(BigDecimal contributionAllValue) {
        return ResultUtil.data(greenCountService.computeBonus(contributionAllValue));
    }
}
